$(function() {

    //获取验证码
    $(document).on('click','#mobileCode' ,function(){
        senderValid();
    });

    //手机登录
    $(document).on('click','.mobileSave' ,function(){
        var mobileusernameinput = $('#mobileusernameinput').val();
        if (mobileusernameinput == "") {
            Prompt.show("姓名不能为空!",'提示',function(){});
            return false;
        }
        var mobileuseridnoinput = $('#mobileuseridnoinput').val();
        if (mobileuseridnoinput == "") {
            Prompt.show("身份证号不能为空!",'提示',function(){});
            return false;
        }
        if(!IdentityCodeValid(mobileuseridnoinput)){
            Prompt.show("身份证格式不正确!",'提示',function(){});
            return false;
        }
        var mobilePhone = $('#mobilePhone').val();
        var filter = /^1\d{10}$/;
        if (mobilePhone == "") {
            Prompt.show("手机号不能为空!",'提示',function(){});
            return false;
        }
        if (filter.test(mobilePhone) == false) {
            Prompt.show("手机号错误!",'提示',function(){});
            return false;
        }
        var mobileValid = $('#mobileValid').val();
        if (mobileValid == "") {
            Prompt.show("手机验证码不能为空!",'提示',function(){});
            return false;
        }
        var mobilePase = $("#mobilePase").val();
        if (mobilePase == "") {
            Prompt.show("密码不能为空!",'提示',function(){});
            return false;
        }

        var mobilePaseagain = $("#mobilePaseagain").val();
        if (mobilePaseagain == "") {
            Prompt.show("确认密码不能为空!",'提示',function(){});
            return false;
        }

        if (mobilePase != mobilePaseagain) {
            Prompt.show("确认密码不正确!",'提示',function(){});
            return false;
        }

        var yanzheng = $('#yanzheng').val();
        if (yanzheng == "") {
            Prompt.show("验证码不能为空!",'提示',function(){});
            return false;
        }

        var checkCode = $('#checkCode').html();

        if (yanzheng.toUpperCase() != checkCode) {
            Prompt.show("验证码不正确!",'提示',function(){});
            return false;
        }
        var hash = $.md5(mobilePase);
        var hashPass = $.md5(mobilePaseagain);
        var data = {
            phone:mobilePhone,
            mobileValid:mobileValid,
            password:hash,
            confimPassword:hashPass,
            userName:mobileusernameinput,
            idNo:mobileuseridnoinput
        }
        app.post(app.url.api_base+"login/registerUserForMobile.do",data,
            function(req) { 
                 if(req.code==0){
                    app.addItem('userName',req.data.userName);
                    app.addItem('userId',req.data.id);
                    app.addItem('userToken',req.data.userToken);
                    app.go('home.html');
                  }else{
                    Prompt.show(req.message)
                  }
            },false);
        changeCode();
    });

    //发送验证码
    function senderValid() {
        var mobilePhone = $('#mobilePhone').val();
        var filter = /^1\d{10}$/;
        if (mobilePhone == "") {
            Prompt.show("手机号不能为空!",'提示',function(){});
            return false;
        }
        if (filter.test(mobilePhone) == false) {
            Prompt.show("手机号错误!",'提示',function(){});
            return false;
        }

        app.post( app.url.api_base+"login/sendMobileValidForCreate.do",{phone:mobilePhone},
            function(req) { 
                Prompt.show(req.message);
            },true);
        setButtonName();
    }

    //验证码计时
    function setButtonName() {
        var num = 60
        clearInterval(timer);
        var timer = setInterval(function() {
            num = num - 1;
            $('#mobileCode').html('请等待(' + num + ')');
            $(document).off('click','#mobileCode' );
            if (num == 0) {
                clearInterval(timer);
                $('#mobileCode').html("获取验证码");
                //获取验证码
                $(document).on('click','#mobileCode' ,function(){
                    senderValid();
                });
            }
        }, 1000)

    }

    // tab切换
    $(document).on('click','.Comtab li',function(){
      $(this).addClass('active').siblings().removeClass('active')
      $(".TabBox>div").eq($(this).index()).show().siblings().hide();
    })


    //btn
    function myBrowser() {
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        var isOpera = userAgent.indexOf("Opera") > -1;
        if (isOpera) {
            return "Opera"
        }; //判断是否Opera浏览器
        if (userAgent.indexOf("Firefox") > -1) {
            return "FF";
        } //判断是否Firefox浏览器
        if (userAgent.indexOf("Chrome") > -1) {
            return "Chrome";
        }
        if (userAgent.indexOf("Safari") > -1) {
            return "Safari";
        } //判断是否Safari浏览器
        if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
            return "IE";
        }; //判断是否IE浏览器
    }

    //以下是调用上面的函数
    var mb = myBrowser();
    if ("Chrome" == mb) {
        new TouchRipple('.butsubmit', {
            color: 'rgba(255,255,255,.2)',
            time: 800,
            size: null
        });
    }



    //点击生产新的验证码
    $(document).on('click', '.YanzLogin', function() {
        changeCode();
    })

    //生成验证码
    changeCode();
    var code;

    function changeCode() {
        code = new Array();
        var codeLength = 4; //验证码的长度
        var checkCode = document.getElementById("checkCode");
        checkCode.value = "";
        var selectChar = new Array(2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        for (var i = 0; i < codeLength; i++) {
            var charIndex = Math.floor(Math.random() * 32);
            code += selectChar[charIndex];
        }
        if (code.length != codeLength) {
            createCode();
        }
        checkCode.innerHTML = code;
    }

    function IdentityCodeValid(code) {
        var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
        var tip = "";
        var pass= true;
        if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
            pass = false;
        } else if(!city[code.substr(0,2)]){
            pass = false;
        } else{
            if(code.length == 18){
                code = code.split('');
                var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
                var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++)
                {
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if(parity[sum % 11] != code[17]){
                    pass =false;
                }
            }
        }
        return pass;
    }

})

